package com.bkgy.controller.qualityDaily.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bkgy.controller.qualityDaily.entity.RepQuaPlank;
import com.bkgy.controller.qualityDaily.entity.pdo.PlankLengzha;
import com.bkgy.controller.qualityDaily.entity.pdo.PlankRepRes;
import com.bkgy.controller.qualityDaily.entity.pdo.QualityReq;
import com.bkgy.controller.qualityDaily.mapper.IQualitydailyPlankRepMapper;
import com.bkgy.controller.qualityDaily.service.IQualitydailyPlankRepService;
import com.bkgy.controller.tPdiDataL3Cgl1870.entity.TPdiDataL3Cgl1870;
import com.bkgy.controller.tPdiDataL3Cgl1870.service.ITPdiDataL3Cgl1870Service;
import com.bkgy.controller.technology.entity.TDefectData;
import com.bkgy.controller.technology.service.ITDefectDataService;
import com.bkgy.sys.common.constant.CommonConstant;
import com.bkgy.sys.common.entity.ResultData;
import com.bkgy.util.DateUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.*;

@Slf4j
@Service
@RequiredArgsConstructor
public class QualitydailyPlankRepServiceImpl extends ServiceImpl<IQualitydailyPlankRepMapper, RepQuaPlank> implements IQualitydailyPlankRepService {
    private final IQualitydailyPlankRepMapper mapper;
    private final ITDefectDataService defectService;
    private final ITPdiDataL3Cgl1870Service pdi1870Service;

    @Override
    public ResultData plankRep(QualityReq req) {
        String[] type1Arr = new String[]{"内板", "内板", "外板", "外板", "外板", "外板"};
        String[] type2Arr = new String[]{"三冷冷轧", "三冷镀锌", "冷轧FC", "冷轧FD", "镀锌FC", "镀锌FD"};
        //初始化结果集
        List<PlankRepRes> list = new ArrayList<>();
        for (int i = 0; i < 6; i++) {
            PlankRepRes res = new PlankRepRes();
            res.setType1(type1Arr[i]);
            res.setType2(type2Arr[i]);
            list.add(res);
        }

        //填充当日数据
        Date today = new Date();
        RepQuaPlank todayData = buildPlankRepData(today);
        for (int i = 0; i < 6; i++) {
            PlankRepRes row = list.get(i);
            for (int j = 0; j < 5; j++) {
                try {
                    BigDecimal newValue = (BigDecimal) todayData.getClass().getMethod("getCol" + i + j).invoke(todayData);
                    row.getClass().getMethod("setDay" + j, BigDecimal.class).invoke(row, newValue);
                } catch (Exception e) {
                    e.printStackTrace();
                    log.error(e.getMessage());
                }
            }
            //综合合格率=合格量/产出量
            if (row.getDay1().compareTo(BigDecimal.ZERO) != 0) {
                row.setDay5(row.getDay2().divide(row.getDay1(), 4, BigDecimal.ROUND_HALF_UP));
                //内部合格率=1-(内部降级量/产出量)
                row.setDay6(row.getDay1().subtract(row.getDay3()).divide(row.getDay1(), 4, BigDecimal.ROUND_HALF_UP));
            }
        }

        //查询历史记录
        LambdaQueryWrapper<RepQuaPlank> wrapper = new LambdaQueryWrapper<>();
        String beginTime = null;
        String endTime = null;
        if (StringUtils.isNotEmpty(req.getDbegin())) {
            beginTime = req.getDbegin();
            wrapper.ge(RepQuaPlank::getYearMonthDay, beginTime);
        } else {
            beginTime = DateUtil.thisMonthFirstDay("yyyy-MM-dd");
            wrapper.ge(RepQuaPlank::getYearMonthDay, beginTime);
        }
        if (StringUtils.isNotEmpty(req.getDend())) {
            endTime = req.getDend();
            wrapper.le(RepQuaPlank::getYearMonthDay, endTime);
        } else {
            endTime = DateUtil.nextMonthFirstDay("yyyy-MM-dd");
            wrapper.le(RepQuaPlank::getYearMonthDay, endTime);
        }

        //查询范围如果包含今日，将今日数据累加到月累计（表里没有当天记录）
        Date d1 = DateUtil.getStringToDate("yyyy-MM-dd", beginTime);
        Date d2 = DateUtil.getStringToDate("yyyy-MM-dd", endTime);
        if (today.getTime() >= d1.getTime() && today.getTime() <= d2.getTime()) {
            for (int i = 0; i < 6; i++) {
                PlankRepRes row = list.get(i);
                for (int j = 0; j < 5; j++) {
                    try {
                        BigDecimal v = (BigDecimal) todayData.getClass().getMethod("getCol" + i + j).invoke(todayData);
                        row.getClass().getMethod("setSum" + j, BigDecimal.class).invoke(row, v);
                    } catch (Exception e) {
                        e.printStackTrace();
                        log.error(e.getMessage());
                    }
                }
            }
        }

        List<RepQuaPlank> dbSumList = list(wrapper);
        //填充累计数据
        for (RepQuaPlank his : dbSumList) {
            for (int i = 0; i < 6; i++) {
                PlankRepRes row = list.get(i);
                for (int j = 0; j < 5; j++) {
                    try {
                        BigDecimal oldValue = (BigDecimal) row.getClass().getMethod("getSum" + j).invoke(row);
                        BigDecimal newValue = (BigDecimal) his.getClass().getMethod("getCol" + i + j).invoke(his);
                        row.getClass().getMethod("setSum" + j, BigDecimal.class).invoke(row, oldValue.add(newValue));
                    } catch (Exception e) {
                        e.printStackTrace();
                        log.error(e.getMessage());
                    }
                }
                //综合合格率=合格量/产出量
                if (row.getSum1().compareTo(BigDecimal.ZERO) != 0) {
                    row.setSum5(row.getSum2().divide(row.getSum1(), 4, BigDecimal.ROUND_HALF_UP));
                    //内部合格率=1-(内部降级量/产出量)
                    row.setSum6(row.getSum1().subtract(row.getSum3()).divide(row.getSum1(), 4, BigDecimal.ROUND_HALF_UP));
                }
            }
        }

        //构建合计行
        //汽车板总计
        PlankRepRes allTotalRow = new PlankRepRes();
        allTotalRow.setType1("汽车板总计");
        allTotalRow.setType2("汽车板总计");
        sumRow(0, 6, list, allTotalRow);
        list.add(allTotalRow);

        //外板合计
        PlankRepRes outTotalRow = new PlankRepRes();
        outTotalRow.setType1("外板");
        outTotalRow.setType2("外板合计");
        sumRow(2, 6, list, outTotalRow);
        list.add(6, outTotalRow);

        //镀锌合计
        PlankRepRes duxinRow = new PlankRepRes();
        duxinRow.setType1("外板");
        duxinRow.setType2("镀锌合计");
        sumRow(4, 6, list, duxinRow);
        list.add(6, duxinRow);

        //冷轧合计
        PlankRepRes lengzhaRow = new PlankRepRes();
        lengzhaRow.setType1("外板");
        lengzhaRow.setType2("冷轧合计");
        sumRow(2, 4, list, lengzhaRow);
        list.add(4, lengzhaRow);

        //内板合计
        PlankRepRes neibanRow = new PlankRepRes();
        neibanRow.setType1("内板");
        neibanRow.setType2("内板合计");
        sumRow(0, 2, list, neibanRow);
        list.add(2, neibanRow);

        return ResultData.ResultDataSuccess(list, list.size());
    }

    //计算几行之间的累加数据
    private void sumRow(int start, int end, List<PlankRepRes> list, PlankRepRes row) {
        for (int i = start; i < end; i++) {
            PlankRepRes currentRow = list.get(i);
            for (int j = 0; j < 5; j++) {
                try {
                    BigDecimal oldValue = (BigDecimal) row.getClass().getMethod("getDay" + j).invoke(row);
                    BigDecimal newValue = (BigDecimal) currentRow.getClass().getMethod("getDay" + j).invoke(currentRow);
                    row.getClass().getMethod("setDay" + j, BigDecimal.class).invoke(row, oldValue.add(newValue));

                    BigDecimal oldValue2 = (BigDecimal) row.getClass().getMethod("getSum" + j).invoke(row);
                    BigDecimal newValue2 = (BigDecimal) currentRow.getClass().getMethod("getSum" + j).invoke(currentRow);
                    row.getClass().getMethod("setSum" + j, BigDecimal.class).invoke(row, oldValue2.add(newValue2));
                } catch (Exception e) {
                    e.printStackTrace();
                    log.error(e.getMessage());
                }
            }
        }
        if (row.getDay1().compareTo(BigDecimal.ZERO) != 0) {
            row.setDay5(row.getDay2().divide(row.getDay1(), 4, BigDecimal.ROUND_HALF_UP));
            row.setDay6(row.getDay1().subtract(row.getDay3()).divide(row.getDay1(), 4, BigDecimal.ROUND_HALF_UP));
        }
        if (row.getSum1().compareTo(BigDecimal.ZERO) != 0) {
            row.setSum5(row.getSum2().divide(row.getSum1(), 4, BigDecimal.ROUND_HALF_UP));
            row.setSum6(row.getSum1().subtract(row.getSum3()).divide(row.getSum1(), 4, BigDecimal.ROUND_HALF_UP));
        }
    }

    @Override
    public boolean buildPlankRepJob(Date date) {
        RepQuaPlank obj = buildPlankRepData(date);
        return save(obj);
    }

    @Override
    public ResultData<JSONObject> plankLineRep() {
        LambdaQueryWrapper<RepQuaPlank> wrapper = new LambdaQueryWrapper<>();
        wrapper.orderByDesc(RepQuaPlank::getYearMonthDay).last(" FETCH FIRST 14 ROWS ONLY");
        List<RepQuaPlank> list = list(wrapper);
        Collections.reverse(list);
        List<String> shijianList = new ArrayList<>();
        //内板合格率
        List<BigDecimal> hegeRateNei = new ArrayList<>();
        //外板合格率
        List<BigDecimal> hegeRateWai = new ArrayList<>();
        for (RepQuaPlank row : list) {
            Date date = DateUtil.getStringToDate("yyyy-MM-dd", row.getYearMonthDay());
            shijianList.add(DateUtil.getDateToString(date, "MM/dd"));

            BigDecimal chanchu1 = row.getCol01().add(row.getCol11());
            if (chanchu1.compareTo(BigDecimal.ZERO) > 0) {
                BigDecimal rateNei = row.getCol02().add(row.getCol12()).multiply(BigDecimal.valueOf(100))
                        .divide(chanchu1, 2, BigDecimal.ROUND_HALF_UP);
                hegeRateNei.add(rateNei);
            } else hegeRateNei.add(BigDecimal.ZERO);

            BigDecimal chanchu2 = row.getCol21().add(row.getCol31()).add(row.getCol41()).add(row.getCol51());
            if (chanchu2.compareTo(BigDecimal.ZERO) > 0) {

                BigDecimal rateWai = row.getCol22().add(row.getCol32()).add(row.getCol42()).add(row.getCol52())
                        .multiply(BigDecimal.valueOf(100))
                        .divide(chanchu2, 2, BigDecimal.ROUND_HALF_UP);
                hegeRateWai.add(rateWai);
            } else hegeRateWai.add(BigDecimal.ZERO);
        }
        JSONObject res = new JSONObject();
        res.put("x", shijianList.toArray(new String[shijianList.size()]));
        res.put("data1", hegeRateNei.toArray(new BigDecimal[hegeRateNei.size()]));
        res.put("data2", hegeRateWai.toArray(new BigDecimal[hegeRateWai.size()]));

        return ResultData.ResultDataSuccess(res);
    }

    //构建某一日的外板报表对象
    private RepQuaPlank buildPlankRepData(Date date) {
        RepQuaPlank res = new RepQuaPlank();
        res.setYearMonthDay(DateUtil.getDateToString(date, "yyyy-MM-dd"));
        Date d1 = DateUtil.getCurrentZero(date);
        String startTime = DateUtil.getDateToString(d1, "yyyyMMddHHmmss");
        Date d2 = DateUtil.getNextDayZero(date);
        String endTime = DateUtil.getDateToString(d2, "yyyyMMddHHmmss");

        //查询三冷冷轧一天内生产数据。
        loadLengzha(startTime, endTime, res);
        //构建镀锌一天生产数据
        loadDuxin(startTime, endTime, res);

        return res;
    }

    //获取三冷冷轧数据
    private void loadLengzha(String startTime, String endTime, RepQuaPlank res) {
        //查询1630、2150产线数据
        BigDecimal touruTotal = BigDecimal.ZERO;
        BigDecimal chanchuTotal = BigDecimal.ZERO;
        Set<String> entidList = new HashSet<>();
        List<String> extidList = new LinkedList<>();
        //出口卷号-出口重量
        Map<String, PlankLengzha> extIdPdoMap = new HashMap<>();
        //查询1630、2150 L3数据，投入量、产出量
        List<PlankLengzha> lengzhaPdoList = mapper.selectLengzhaPdo(startTime, endTime);
        Map<String, BigDecimal> touruMap = new HashMap<>();
        for (PlankLengzha temp : lengzhaPdoList) {
            entidList.add(temp.getEntid());//入口卷号
            extidList.add(temp.getExtid());//出口卷号
            touruMap.put(temp.getEntid(), temp.getInMatWt());
            chanchuTotal = chanchuTotal.add(temp.getOutMatWt());//产出量累计
            extIdPdoMap.put(temp.getExtid(), temp);
        }
        //投入量累计。由于分卷，过滤投入量，避免重复计算
        for (Map.Entry<String, BigDecimal> entry : touruMap.entrySet()) {
            touruTotal = touruTotal.add(entry.getValue());
        }

        //废品0|00、次材10、降级品合计重
        BigDecimal feicijiangTotal = BigDecimal.ZERO;
        //废品0|00、次材10、降级品，归户到内部的重量
        BigDecimal feicijiangTotalIn = BigDecimal.ZERO;

        //同一轧卷只保留最新缺陷
        LambdaQueryWrapper<TDefectData> w = new LambdaQueryWrapper();
        Map<String, TDefectData> defectMap = new HashMap<>();
        w.in(TDefectData::getExtId, extidList).orderByAsc(TDefectData::getDefectVersionTime1);
        List<TDefectData> defectList = defectService.list(w);
        for (TDefectData defectData : defectList) {
            defectMap.put(defectData.getExtId(), defectData);
        }

        BigDecimal fcTouruTotal = BigDecimal.ZERO;
        BigDecimal fcChanchuTotal = BigDecimal.ZERO;
        BigDecimal fcFeicjInTotal = BigDecimal.ZERO;
        BigDecimal fcFeicjOutTotal = BigDecimal.ZERO;
        BigDecimal fdTouruTotal = BigDecimal.ZERO;
        BigDecimal fdChanchuTotal = BigDecimal.ZERO;
        BigDecimal fdFeicjInTotal = BigDecimal.ZERO;
        BigDecimal fdFeicjOutTotal = BigDecimal.ZERO;

        //分析缺陷，TODO 分析FC、FD
        for (TDefectData defect : defectMap.values()) {
            String extid = defect.getExtId();
            String defectClass = defect.getDefectClass1();//缺陷等级
            String defectComment = defect.getDefectComment1();//缺陷注释
            String guihuStr = CommonConstant.tPdi1870HomeMap.get(defect.getDefectAttributionCode1());//归户信息
            //废品次材
            if ("0".equals(defectClass) || "00".equals(defectClass) || "10".equals(defectClass)) {
                feicijiangTotal = feicijiangTotal.add(extIdPdoMap.get(extid).getOutMatWt());
                if (isBelongSanleng(guihuStr)) {
                    feicijiangTotalIn = feicijiangTotalIn.add(extIdPdoMap.get(extid).getOutMatWt());
                }
            } else {
                //TODO 降级品
            }
        }

        //三冷冷轧行数据
        res.setCol00(touruTotal);//投入量
        res.setCol01(chanchuTotal);//产出量
        res.setCol02(chanchuTotal.subtract(feicijiangTotal));//合格量=产出量-废材次材降级品
        res.setCol03(feicijiangTotalIn);//内部降级量
        res.setCol04(feicijiangTotal.subtract(feicijiangTotalIn));//外部降级量
        if (res.getCol01().compareTo(BigDecimal.ZERO) != 0) {
            //综合合格率=合格量/产出量
            res.setCol05(res.getCol02().divide(res.getCol01(), 4, BigDecimal.ROUND_HALF_UP));
            //内部合格率=(产出量-排除量)/产出量
            res.setCol06(res.getCol01().subtract(feicijiangTotal).divide(res.getCol01(), 4, BigDecimal.ROUND_HALF_UP));
        }

        //冷轧FC
        res.setCol20(fcTouruTotal);
        res.setCol21(fcChanchuTotal);
        res.setCol22(fcChanchuTotal.subtract(fcFeicjInTotal).subtract(fcFeicjOutTotal));
        res.setCol23(fcFeicjInTotal);
        res.setCol24(fcFeicjOutTotal);
        if (res.getCol21().compareTo(BigDecimal.ZERO) != 0) {
            res.setCol25(res.getCol22().divide(res.getCol21(), 4, BigDecimal.ROUND_HALF_UP));
            res.setCol26(res.getCol21().subtract(fcFeicjInTotal).subtract(fcFeicjOutTotal).divide(res.getCol21(), 4, BigDecimal.ROUND_HALF_UP));
        }

        //冷轧FD
        res.setCol30(fdTouruTotal);
        res.setCol31(fdChanchuTotal);
        res.setCol32(fdChanchuTotal.subtract(fdFeicjInTotal).subtract(fdFeicjOutTotal));
        res.setCol33(fdFeicjInTotal);
        res.setCol34(fdFeicjOutTotal);
        if (res.getCol31().compareTo(BigDecimal.ZERO) != 0) {
            res.setCol35(res.getCol32().divide(res.getCol31(), 4, BigDecimal.ROUND_HALF_UP));
            res.setCol36(res.getCol31().subtract(fdFeicjInTotal).subtract(fdFeicjOutTotal).divide(res.getCol31(), 4, BigDecimal.ROUND_HALF_UP));
        }
    }

    /**
     * 判断归户是否属于三冷内部
     */
    private boolean isBelongSanleng(String guihuStr) {
        boolean flag = false;
        if (StringUtils.isNotEmpty(guihuStr)) {
            if (guihuStr.contains("三冷") && guihuStr.indexOf("三冷") < 5)
                flag = true;
            if (guihuStr.contains("磨辊") || guihuStr.contains("公辅") || guihuStr.contains("运转"))
                flag = true;
        }

        return flag;
    }

    //构建镀锌部分数据
    private void loadDuxin(String startTime, String endTime, RepQuaPlank res) {
        //查询1870产线数据
        BigDecimal touruSum = BigDecimal.ZERO;
        BigDecimal chanchuSum = BigDecimal.ZERO;
        Set<String> entidList = new HashSet<>();
        List<String> extidList = new LinkedList<>();
        Map<String, BigDecimal> touruMap = new HashMap<>();
        Map<String, PlankLengzha> outMatWtMap = new HashMap<>();
        //查询实际数据，投入量、产出量
        List<PlankLengzha> list = mapper.selectLengzha1870Pdo(startTime, endTime);
        for (PlankLengzha temp : list) {
            entidList.add(temp.getEntid());//入口卷号
            extidList.add(temp.getExtid());//出口卷号
            touruMap.put(temp.getEntid(), temp.getInMatWt());
            chanchuSum = chanchuSum.add(temp.getOutMatWt());//产出量累计
            outMatWtMap.put(temp.getExtid(), temp);
        }
        LambdaQueryWrapper<TPdiDataL3Cgl1870> w1 = new LambdaQueryWrapper<>();
        w1.in(TPdiDataL3Cgl1870::getEntid, entidList);
        List<TPdiDataL3Cgl1870> pdiList = pdi1870Service.list(w1);
        Map<String, String> surfaceMap = new HashMap<>();
        for (TPdiDataL3Cgl1870 pdi : pdiList) {
            surfaceMap.put(pdi.getEntid(), pdi.getSurfaceCode());
        }

        for (Map.Entry<String, BigDecimal> entry : touruMap.entrySet()) {
            touruSum = touruSum.add(entry.getValue());//投入量累计
        }

        //查询缺陷
        //排除的重量（废品0|00、次材10、降级品）
        BigDecimal feicjTotal = BigDecimal.ZERO;
        BigDecimal feicjTotalIn = BigDecimal.ZERO;//归户内部的排除重量

        //同一轧卷只保留最新缺陷
        LambdaQueryWrapper<TDefectData> w = new LambdaQueryWrapper();
        Map<String, TDefectData> defectMap = new HashMap<>();
        w.in(TDefectData::getExtId, extidList).orderByAsc(TDefectData::getDefectVersionTime1);
        List<TDefectData> defectList = defectService.list(w);
        for (TDefectData defectData : defectList) {
            defectMap.put(defectData.getExtId(), defectData);
        }

        BigDecimal fcTouruTotal = BigDecimal.ZERO;
        BigDecimal fcChanchuTotal = BigDecimal.ZERO;
        BigDecimal fcFeicjTotal = BigDecimal.ZERO;
        BigDecimal fcFeicjInTotal = BigDecimal.ZERO;
        BigDecimal fdTouruTotal = BigDecimal.ZERO;
        BigDecimal fdChanchuTotal = BigDecimal.ZERO;
        BigDecimal fdFeicjTotal = BigDecimal.ZERO;
        BigDecimal fdFeicjInTotal = BigDecimal.ZERO;

        //分析缺陷
        for (TDefectData defect : defectMap.values()) {
            String extid = defect.getExtId();
            String entid = outMatWtMap.get(extid).getEntid();
            String defectClass = defect.getDefectClass1();//缺陷等级
            String defectComment = defect.getDefectComment1();//缺陷注释
            String guihuStr = CommonConstant.tPdi1870HomeMap.get(defect.getDefectAttributionCode1());//归户信息
            PlankLengzha pdo = outMatWtMap.get(extid);
            BigDecimal outMatWt = outMatWtMap.get(extid).getOutMatWt();
            //是否属于降级
            boolean down = !surfaceMap.get(entid).equals(outMatWtMap.get(extid).getSurfaceCode());
            //镀锌FC
            if (StringUtils.isNotEmpty(defectComment)) {
                if (defectComment.startsWith("FC")) {
                    fcTouruTotal = fcTouruTotal.add(pdo.getInMatWt());
                    fcChanchuTotal = fcChanchuTotal.add(pdo.getOutMatWt());

                    if ("0".equals(defectClass) || "00".equals(defectClass) || "10".equals(defectClass) || down) {
                        feicjTotal = feicjTotal.add(outMatWt);
                        fcFeicjTotal = fcFeicjTotal.add(outMatWt);
                        if (isBelongSanleng(guihuStr)) {
                            feicjTotalIn = feicjTotalIn.add(outMatWt);
                            fcFeicjInTotal = fcFeicjInTotal.add(outMatWt);
                        }
                    }
                } else if (defectComment.startsWith("FD")) {
                    fdTouruTotal = fdTouruTotal.add(pdo.getInMatWt());
                    fdChanchuTotal = fdChanchuTotal.add(pdo.getOutMatWt());

                    if ("0".equals(defectClass) || "00".equals(defectClass) || "10".equals(defectClass) || down) {
                        feicjTotal = feicjTotal.add(outMatWt);
                        fdFeicjTotal = fdFeicjTotal.add(outMatWt);
                        if (isBelongSanleng(guihuStr)) {
                            feicjTotalIn = feicjTotalIn.add(outMatWt);
                            fdFeicjInTotal = fdFeicjInTotal.add(outMatWt);
                        }
                    }
                } else {
                    if ("0".equals(defectClass) || "00".equals(defectClass) || "10".equals(defectClass) || down) {
                        feicjTotal = feicjTotal.add(outMatWt);
                        if (isBelongSanleng(guihuStr)) {
                            feicjTotalIn = feicjTotalIn.add(outMatWt);
                        }
                    }
                }
            }
        }

        //三冷镀锌
        res.setCol10(touruSum);//投入量
        res.setCol11(chanchuSum);//产出量
        res.setCol12(chanchuSum.subtract(feicjTotal));//合格量=产出量-废材次材降级品
        res.setCol13(feicjTotalIn);//内部降级量
        res.setCol14(feicjTotal.subtract(feicjTotalIn));//外部降级量
        if (res.getCol11().compareTo(BigDecimal.ZERO) != 0) {
            //综合合格率=合格量/产出量
            res.setCol15(res.getCol12().divide(res.getCol11(), 4, BigDecimal.ROUND_HALF_UP));
            //内部合格率=(产出量-排除量)/产出量
            res.setCol16(res.getCol11().subtract(feicjTotal).divide(res.getCol11(), 4, BigDecimal.ROUND_HALF_UP));
        }

        //镀锌FC、FD
        res.setCol40(fcTouruTotal);
        res.setCol41(fcChanchuTotal);
        res.setCol42(fcChanchuTotal.subtract(fcFeicjTotal));
        res.setCol43(fcFeicjInTotal);
        res.setCol44(fcFeicjTotal.subtract(fcFeicjInTotal));
        if (res.getCol41().compareTo(BigDecimal.ZERO) != 0) {
            res.setCol45(res.getCol42().divide(res.getCol41(), 4, BigDecimal.ROUND_HALF_UP));
            res.setCol46(res.getCol41().subtract(fcFeicjTotal).divide(res.getCol41(), 4, BigDecimal.ROUND_HALF_UP));
        }

        res.setCol50(fdTouruTotal);
        res.setCol51(fdChanchuTotal);
        res.setCol52(fdChanchuTotal.subtract(fdFeicjTotal));
        res.setCol53(fcFeicjInTotal);
        res.setCol54(fcFeicjTotal.subtract(fcFeicjInTotal));
        if (res.getCol51().compareTo(BigDecimal.ZERO) != 0) {
            res.setCol55(res.getCol52().divide(res.getCol51(), 4, BigDecimal.ROUND_HALF_UP));
            res.setCol56(res.getCol51().subtract(fdFeicjTotal).divide(res.getCol51(), 4, BigDecimal.ROUND_HALF_UP));
        }
    }
}
